package com.bank.ui.admin;

import com.bank.dao.CardDAO;
import com.bank.entity.Card;
import com.bank.dao.CSVExporter;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.SQLException;
import java.util.List;

public class CardManagementPanel extends JPanel {
    private JTable cardTable;

    public CardManagementPanel() {
        setLayout(new BorderLayout());

        // 查询并列出所有银行卡信息
        try {
            CardDAO cardDAO = new CardDAO();
            List<Card> cards = cardDAO.getAllCards();

            String[] columnNames = {"卡号", "存款类型", "开户日期", "开户金额", "余额", "密码", "是否挂失", "客户编号"};
            Object[][] data = new Object[cards.size()][columnNames.length];
            for (int i = 0; i < cards.size(); i++) {
                Card card = cards.get(i);
                data[i][0] = card.getCardID();
                data[i][1] = card.getSavingID();
                data[i][2] = card.getOpenDate();
                data[i][3] = card.getOpenMoney();
                data[i][4] = card.getBalance();
                data[i][5] = card.getPassword();
                data[i][6] = card.getIsReportLoss();
                data[i][7] = card.getCustomerID();
            }

            cardTable = new JTable(data, columnNames);
            JScrollPane scrollPane = new JScrollPane(cardTable);
            add(scrollPane, BorderLayout.CENTER);

            // 操作按钮面板
            JPanel buttonPanel = new JPanel();

            // 挂失按钮
            JButton reportLossButton = new JButton("挂失银行卡");
            reportLossButton.addActionListener(e -> {
                int selectedRow = cardTable.getSelectedRow();
                if (selectedRow != -1) {
                    String cardID = (String) cardTable.getValueAt(selectedRow, 0);
                    try {
                        CardDAO dao = new CardDAO();
                        // 这里假设挂失操作是将 IsReportLoss 设为 '是'
                        Card card = dao.getCardByID(cardID);
                        card.setIsReportLoss("是");
                        dao.updateCard(card);
                        refreshTable();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(this, "挂失银行卡失败！", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "请选择要挂失的银行卡！", "提示", JOptionPane.WARNING_MESSAGE);
                }
            });
            buttonPanel.add(reportLossButton);

            // 修改密码按钮
            JButton changePasswordButton = new JButton("修改密码");
            changePasswordButton.addActionListener(e -> {
                int selectedRow = cardTable.getSelectedRow();
                if (selectedRow != -1) {
                    String cardID = (String) cardTable.getValueAt(selectedRow, 0);
                    String oldPassword = JOptionPane.showInputDialog(this, "请输入旧密码:");
                    String newPassword = JOptionPane.showInputDialog(this, "请输入新密码:");
                    try {
                        CardDAO dao = new CardDAO();
                        if (dao.changePassword(cardID, oldPassword, newPassword)) {
                            JOptionPane.showMessageDialog(this, "密码修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                            refreshTable();
                        } else {
                            JOptionPane.showMessageDialog(this, "密码修改失败！", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(this, "密码修改失败！", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "请选择要修改密码的银行卡！", "提示", JOptionPane.WARNING_MESSAGE);
                }
            });
            buttonPanel.add(changePasswordButton);

            // 修改银行卡信息按钮
            JButton editButton = new JButton("修改银行卡信息");
            editButton.addActionListener(e -> {
                int selectedRow = cardTable.getSelectedRow();
                if (selectedRow != -1) {
                    String cardID = (String) cardTable.getValueAt(selectedRow, 0);
                    new EditCardDialog(cardID).setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(this, "请选择要修改的银行卡！", "提示", JOptionPane.WARNING_MESSAGE);
                }
            });
            buttonPanel.add(editButton);

            // 删除银行卡按钮
            JButton deleteButton = new JButton("删除银行卡");
            deleteButton.addActionListener(e -> {
                int selectedRow = cardTable.getSelectedRow();
                if (selectedRow != -1) {
                    String cardID = (String) cardTable.getValueAt(selectedRow, 0);
                    int confirm = JOptionPane.showConfirmDialog(this, "确定要删除该银行卡吗？", "确认删除", JOptionPane.YES_NO_OPTION);
                    if (confirm == JOptionPane.YES_OPTION) {
                        try {
                            CardDAO dao = new CardDAO();
                            dao.deleteCard(cardID);
                            refreshTable();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                            JOptionPane.showMessageDialog(this, "删除银行卡失败！", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "请选择要删除的银行卡！", "提示", JOptionPane.WARNING_MESSAGE);
                }
            });
            buttonPanel.add(deleteButton);

            // 导出银行卡数据到 CSV 报表
            JButton exportButton = new JButton("导出银行卡数据");
            exportButton.addActionListener(e -> {
                try {
                    CSVExporter.exportCardsToCSV(cards, "cards.csv");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "导出银行卡数据时发生错误: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
            buttonPanel.add(exportButton);

            add(buttonPanel, BorderLayout.SOUTH);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void refreshTable() {
        try {
            CardDAO cardDAO = new CardDAO();
            List<Card> cards = cardDAO.getAllCards();
            String[] columnNames = {"卡号", "存款类型", "开户日期", "开户金额", "余额", "密码", "是否挂失", "客户编号"};
            Object[][] data = new Object[cards.size()][columnNames.length];
            for (int i = 0; i < cards.size(); i++) {
                Card card = cards.get(i);
                data[i][0] = card.getCardID();
                data[i][1] = card.getSavingID();
                data[i][2] = card.getOpenDate();
                data[i][3] = card.getOpenMoney();
                data[i][4] = card.getBalance();
                data[i][5] = card.getPassword();
                data[i][6] = card.getIsReportLoss();
                data[i][7] = card.getCustomerID();
            }
            cardTable.setModel(new DefaultTableModel(data, columnNames));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}